//
//  CodeTableViewController.m
//  GitBucket
//
//  Created by 3024 on 15/11/13.
//  Copyright © 2015年 ios25. All rights reserved.
//

#import "CodeTableViewController.h"
#import "AllViewViewController.h"
@interface CodeTableViewController ()

{
    NSString *url_str;
    
    //选择的indexPath的row
    NSIndexPath *aindexPath;
}
@end

@implementation CodeTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    NSLog(@"data_arr:%@",self.data_arr);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.data_arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    NSString *str = _data_arr[indexPath.row][@"type"];
    if (![str isEqualToString:@"dir"]) {
       cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    }else{
        cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        
    }
    cell.textLabel.text = _data_arr[indexPath.row][@"name"];
    return cell;
}

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     aindexPath = indexPath;
    return indexPath;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if ([_data_arr[indexPath.row][@"type"] isEqualToString:@"dir"]) {
        CodeTableViewController *nextCtvc = [self.storyboard instantiateViewControllerWithIdentifier:@"CodeTableViewController"];
        nextCtvc.repoLabel.text = cell.textLabel.text;
        nextCtvc.ownerLabel.text = self.ownerLabel.text;
        
        NSString *url_string = _data_arr[indexPath.row][@"url"];

        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url_string]];

        nextCtvc.data_arr = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        
        [self.navigationController pushViewController:nextCtvc animated:YES];
    }
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
   
        UITableViewCell *cell = sender;
        
        AllViewViewController *arvc = segue.destinationViewController;
        arvc.repoLabel.text = cell.textLabel.text;
        arvc.ownerLabel.text = @"master";
        arvc.url_string = [NSString stringWithFormat:@"%@?client_id=7222dd6497b25737c70c&client_secret=b507ccb75cd6b99d6759a79ee0b406d13294f4fa",_data_arr[aindexPath.row][@"html_url"]];
        NSLog(@"arvc.url_string%@",arvc.url_string);
    
    
}


@end
